<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Coupon\CouponSendAction;
use App\Admin\Actions\Coupon\CouponStopAction;
use App\Library\Traits\AdminControllerTrait;
use App\Models\CouponModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Grid\Displayers\Actions;

class CouponController extends AdminController
{
    use AdminControllerTrait;

    protected $title = '优惠券';

    /**
     * grid
     * @return Grid
     */
    protected function grid(): Grid
    {
        $grid = new Grid(new CouponModel);
        $grid->model()->orderBy('sort')->orderByDesc('created_at');
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('id')->integer();
            });
        });
        $grid->column('id', 'ID')->sortable();
        $grid->column('serial_number', __('l.serialNumber'));
        $grid->column('title', __('l.title'));
        $grid->column('sort', __('l.sort'))->sortable();
        $grid->column('status', __('l.status'))->using(__('coupon.statusArr'));
        $grid->column('is_system', '系统优惠券')->using(__('l.whether'));
        $grid->column('amount', __('coupon.amount'));
        $grid->column('full_amount', __('coupon.full_amount'));
        $grid->column('start_at', __('l.startAt'));
        $grid->column('end_at', __('l.endAt'));
        $grid->column('quantity', __('coupon.quantity'));
        $grid->column('get_count', __('coupon.get_count'));
        $grid->column('receive_count', __('coupon.receive_count'));
        $grid->column('use_count', __('coupon.use_count'));
        $grid->column('send_start_at', __('coupon.send_start_at'));
        $grid->column('send_end_at', __('coupon.send_end_at'));
        $grid->column('send_complete_at', __('coupon.send_complete_at'));
        $grid->disableCreateButton(false);
        $grid->actions(function (Actions $action) {
            $action->disableEdit($action->row->status !== 0);
            $action->disableDelete();
            $action->disableView();
            if (in_array($action->row->status, [0, 2])) $action->add(new CouponSendAction);
            else if ($action->row->status === 1) $action->add(new CouponStopAction);
        });
        $grid->disableActions(false);
        return $grid;
    }

    /**
     * form
     * @return Form
     */
    public function form(): Form
    {
        $form = new Form(new CouponModel);
        $form->hidden('serial_number');
        $form->hidden('status')->default(0);
        $form->hidden('is_system')->default(0);
        $form->column(1 / 2, function (Form $form) {
            $form->text('title', __('l.title'))->default(random())->attribute('maxlength', 30)->required();
            $form->currency('full_amount', __('coupon.full_amount'))->symbol('￥');
            $form->datetime('start_at', __('coupon.start_at'));
            $form->select('get_condition', __('coupon.get_condition'))->default(0)
                ->options([0 => '所有人', 1 => '新用户'])->required();
        });
        $form->column(1 / 2, function (Form $form) {
            $form->number('quantity', __('coupon.quantity'))->default(0)->min(0)->max(9999)->required();
            $form->currency('amount', __('coupon.amount'))->default(1)->symbol('￥')->required()->rules('required|numeric');
            $form->datetime('end_at', __('coupon.end_at'));
            $form->number('get_count', __('coupon.get_count'))->default(1)->min(1)->max(9999)->required();
        });
        $form->saving(function (Form $form) {
            if ($form->isCreating()) $form->serial_number = serialNumber();
            if (!is_null($form->amount) && $form->amount < 0) return $this->backWith('优惠券金额不能小于0');
            if (!is_null($form->discount) && $form->discount < 0) return $this->backWith('折扣不能小于0');
            if ((int)$form->get_condition === 1) $form->is_system = 1;
            $form->publish_id = Admin::user()->user_id;
            return $form;
        });
        $form->setWidth(10);
        return $form;
    }
}
